package com.enjoylearning.media.service.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @description 测试执行器
 * @author Mr.M
 * @date 2022/9/13 20:32
 * @version 1.0
 */
 @Component
 @Slf4j
public class SampleJob {

 /**
  * 1、简单任务示例（Bean模式）
  */
 @XxlJob("fanxt")
 public void testJob() throws Exception {
  log.info("开始执行.....");

 }

 @XxlJob("shardingJobHandler")
 public void shardingJobHandler() throws Exception {

  // 分片参数
  int shardIndex = XxlJobHelper.getShardIndex();
  int shardTotal = XxlJobHelper.getShardTotal();

  XxlJobHelper.log("分片参数：当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);

  // 业务逻辑
  for (int i = 0; i < shardTotal; i++) {
   if (i == shardIndex) {
    XxlJobHelper.log("第 {} 片, 命中分片开始处理", i);
   } else {
    XxlJobHelper.log("第 {} 片, 忽略", i);
   }
  }

 }

}